package com.example.showlocation;

import android.annotation.SuppressLint;
import android.app.PendingIntent;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

import com.tencent.map.geolocation.TencentLocation;
import com.tencent.map.geolocation.TencentLocationListener;
import com.tencent.map.geolocation.TencentLocationManager;
import com.tencent.map.geolocation.TencentLocationRequest;
import com.tencent.tencentmap.mapsdk.maps.CameraUpdate;
import com.tencent.tencentmap.mapsdk.maps.CameraUpdateFactory;
import com.tencent.tencentmap.mapsdk.maps.TencentMap;
import com.tencent.tencentmap.mapsdk.maps.TencentMapInitializer;
import com.tencent.tencentmap.mapsdk.maps.TextureMapView;
import com.tencent.tencentmap.mapsdk.maps.model.CameraPosition;
import com.tencent.tencentmap.mapsdk.maps.model.LatLng;

import java.io.IOException;

public class TextureMapViewActivity extends AppCompatActivity {
    TextureMapView textureMapView;
    TencentMap mTencentMap;
    TencentLocation tencentLocation;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_texture_map_view);
        TencentMapInitializer.setAgreePrivacy(true);
        textureMapView=(TextureMapView)findViewById(R.id.view2) ;
        mTencentMap = textureMapView.getMap();
    }
    @SuppressLint("IntentReset")
    public void NORMAL(View view) throws IOException, PendingIntent.CanceledException {
mTencentMap.setMapType(TencentMap.MAP_TYPE_NORMAL);
        Log.d(getLocalClassName()+":", "NORMAL(View view)");
    }


    @SuppressLint("IntentReset")
    public void SATELLITE(View view) throws IOException, PendingIntent.CanceledException {
        mTencentMap.setMapType(TencentMap.MAP_TYPE_SATELLITE);
        Log.d(getLocalClassName()+":", "SATELLITE(View view)");
    }
    @SuppressLint("IntentReset")
    public void DARK(View view) throws IOException, PendingIntent.CanceledException {
        mTencentMap.setMapType(TencentMap.MAP_TYPE_DARK);
        Log.d(getLocalClassName()+":", "DARK(View view)");
    }
    @SuppressLint("IntentReset")
    public void CameraUpdate(View view) throws Exception {
        try {
            AddLocation_Tencent();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        Log.d(getLocalClassName()+":", "CameraUpdate(View view)");
    }
    private void AddLocation_Tencent() throws Exception {

        TencentLocationManager mLocationManager = TencentLocationManager.getInstance(this);
        TencentLocationManager.setUserAgreePrivacy(true);
        mLocationManager.setDeviceID(getApplicationContext(),DeviceIdUtil.getDeviceId(this));
        Log.d("getDeviceId()"+":", DeviceIdUtil.getDeviceId(this));
        class MyLocationListener implements TencentLocationListener {
            @Override
            public void onLocationChanged(TencentLocation location, int error, String reason) {

                CameraUpdate cameraSigma =
                        CameraUpdateFactory.newCameraPosition(new CameraPosition(
                                new LatLng(location.getLatitude(),location.getLongitude()), //中心点坐标，地图目标经纬度
                                19,  //目标缩放级别
                                45f, //目标倾斜角[0.0 ~ 45.0] (垂直地图时为0)
                                45f)); //目标旋转角 0~360° (正北方为0)
                mTencentMap.moveCamera(cameraSigma);
                Log.d(getLocalClassName()+":", "onLocationChanged");
                Log.d(getLocalClassName()+":", location.getLatitude()+","+location.getLongitude());
            }

            @Override
            public void onStatusUpdate(String name, int status, String desc) {
                /**
                 * name - 设备名, GPS, WIFI, CELL 中的某个
                 * status - 状态码, STATUS_ENABLED, STATUS_DISABLED, STATUS_UNKNOWN, STATUS_GPS_AVAILABLE, STATUS_GPS_UNAVAILABLE, STATUS_DENIED中的某个 在使用status之前,请先按照name进行区分
                 * desc - 状态描述
                 */
            }

        }
        MyLocationListener mLocationListener=new MyLocationListener() ;
        TencentLocationRequest request = TencentLocationRequest.create();
        request.setRequestLevel( TencentLocationRequest.REQUEST_LEVEL_POI);
        request.setGpsFirst(true);
        request.setInterval(1000*30000);
        //mLocationManager.requestLocationUpdates(request,mLocationListener);
        mLocationManager.requestSingleFreshLocation(request , mLocationListener, Looper.getMainLooper());
    }
}